#include "logistics.h"

Goods goods[MAX_GOODS];
size_t cnt_goods;
//查找商品ID
int query_goods(const char* id)
{
	for(int i=0;i<cnt_goods;i++)
	{
		if(0==strcmp(goods[i].ID,id))
		{
			return i;
		}
	}
	return -1;
}
//添加商品
void enroll_goods(void)
{
	if(cnt_goods>=MAX_GOODS)
	{
		print_sec("仓库已满！",0.75);
		return;
	}
	printf("进货的第%d种商品\n",cnt_goods+1);
	printf("\n请输入商品ID:");
	get_str(goods[cnt_goods].ID,sizeof(goods[cnt_goods].ID));
	int index=query_goods(goods[cnt_goods].ID);
	if(-1<index)
	{
		print_sec("商品已录入，添加货物即可",0.75);
		return;
	}
	printf("\n请输入商品名称:");
	get_str(goods[cnt_goods].goods_name,sizeof(goods[cnt_goods].goods_name));
	int reexpiration;
	printf("\n请输入商品保质期:");
	scanf("%d",&reexpiration);
	goods[cnt_goods].expiration=reexpiration;
	float rebuy_price;
	printf("\n请输入商品进价:");
	scanf("%f",&rebuy_price);
	goods[cnt_goods].buy_price=rebuy_price;
	float resell_price;
	printf("\n请输入商品售价:");
	scanf("%f",&resell_price);
	goods[cnt_goods].sell_price=resell_price;
	int renum;
	printf("请输入商品数量:");
	scanf("%d",&renum);
	goods[cnt_goods].num=renum;
	cnt_goods++;
	print_sec("添加商品成功！",0.75);
}

//下架商品
void remove_goods(void)        
{
	char reID[10];
	printf("输入要删除的商品ID:");
	get_str(reID,sizeof(reID));
	int index=query_goods(reID);
	if(0<index)
	{
		goods[index] = goods[cnt_goods-1];
    	cnt_goods--;
		print_sec("下架成功",0.75);
		return;
	}
	else
	{
    	print_sec("没有该商品，下架失败",0.75);
		return;
	}
}

//补货
void fill_goods(void)    
{
	char reID[10];
	printf("输入要补货的商品ID：");
	get_str(reID,sizeof(reID));
	int index=query_goods(reID);
	if(0<index)
	{
		printf("请输入补货数量：");
		int renum;
		scanf("%d",&renum);
		goods[index].num+=renum;
		print_sec("补货完成！",0.75);
		return;
	}
	else
	{
		print_sec("还未添加此商品！",0.75);
		return;
	}
}

//报损
void loss_goods(void)         
{
	for(int i = 0 ;i <cnt_goods; i++)
	{
    	if(0 >= goods[i].expiration)
		{
			goods[i] = goods[cnt_goods-1];
			printf("ID为%s的商品过期",goods[i].ID);
			cnt_goods--;
		}
	}
	anykey_continue();
	print_sec("下架成功",0.75);
}

void modify_buy_price(int index)
{
	float rebuy_price;
	printf("请输入新的商品进价：");
	scanf("%f",&rebuy_price);
	goods[index].buy_price = rebuy_price;
}

void modify_sell_price(int index)
{
	float resell_price;
	printf("请输入新的商品售价：");
	scanf("%f",&resell_price);
	goods[index].sell_price = resell_price;
}

void modify_goods_name(int index)
{
	char regoods_name[20];
	printf("请输入新的商品名字：");
	scanf("%s",regoods_name);
	strcpy(goods[index].goods_name,regoods_name);
}

//修改商品信息
void modify_goods()           
{
	char reID[10];
	printf("请输入需要修改商品的ID：");
	get_str(reID,sizeof(reID));
	int index=query_goods(reID);
	if(-1<index)
	{
		for(;;)
		{
			system("clear");
			puts("1、修改商品进价");
			puts("2、修改商品售价");
			puts("3、修改商品名称");
			puts("0、返回");
			switch(get_cmd('0','9'))
			{
	    		case '1': modify_buy_price(index);break;
	    		case '2': modify_sell_price(index); break;
	    		case '3': modify_goods_name(index); break;
	    		case '0': return;
			}
		}
	}
}

//遍历
void show_goods(void)   
{
	printf("商品ID  商品名字  商品进价  商品售价  商品数量  商品保质期\n");
	for(int i=0; i<cnt_goods; i++)
	{
        	printf("%s      %4s        %.2f      %.2f      %d           %d\n",
				goods[i].ID,
				goods[i].goods_name,
				goods[i].buy_price,
				goods[i].sell_price,
				goods[i].num,
				goods[i].expiration);
	}
	anykey_continue();
}

//查看需要补货
void show_goods_num(void)  
{
	for(int i = 0; i<cnt_goods ;i++)
	{
		if(0 <= goods[i].num)
		{		
			printf("需要补货的商品ID为： %s\n",goods[i].ID);
		}
	}
	anykey_continue();
}

//后勤界面
void menu_logistics_(void)
{
	for(;;)
	{
		system("clear");
		puts("*****超市后勤管理系统*****");
		puts("1、添加新商品");
		puts("2、下架商品");
		puts("3、补货");
		puts("4、遍历所有商品");
		puts("5、商品报损");
		puts("6、修改商品信息");
		puts("7、查看需要补货的商品");
		puts("0、退出系统");
		switch(get_cmd('0','9'))
		{
	    	case '1': enroll_goods();break;
	    	case '2': remove_goods();break;
	    	case '3': fill_goods();break;
	    	case '4': show_goods();break;
	    	case '5': loss_goods();break;
	    	case '6': modify_goods();break;
	    	case '7': show_goods_num();break;
	    	case '0': return;
		}
	}
}
//登录后勤
void login_logistics(void)
{
	//输入用户名
	char name[20];
	printf("请输入用户名：");
	get_str(name,sizeof(name));

	//用户名是否存在
	int index=query_logistics(name);
	if(-1==index)
	{
		print_sec("用户名不存在！",0.75);
		return;
	}

	// 输入密码
	char repasswd[7];
	printf("请输入输入密码：");
	get_passwd(repasswd,sizeof(repasswd));

	//密码是否正确
	if(0==strcmp(logistics[index].passwd,repasswd))
	{
		print_sec("登陆成功！",0.75);
		menu_logistics_();
		return;
	}
	else
	{
		print_sec("密码错误，登录失败！",0.75);
		return;
	}
}
